package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {
    @Reference
    private IUserInfoService userInfoService;


    @RequestMapping("/checkPhone")
    @ResponseBody
    public Object checkPhone(String phone){
        return !userInfoService.checkPhone(phone);
    }

    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone);
        AjaxResult result = new AjaxResult();
        return result;
    }

    @RequestMapping("/userRegist")
    @ResponseBody
    public Object userRegist(String phone,String nickname,String password,String rpassword,String verifyCode){
        userInfoService.userRegist(phone, nickname, password, rpassword, verifyCode);
        return new AjaxResult();
    }

    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(String username, String password, HttpServletResponse response){
        //接收用户输入的账号密码,去数据库查该账户是否存在
        String token = userInfoService.userLogin(username, password);
        //注意设置有效的时间,cookie中时间为int类型,把rediskeys中设置的long类型转换
        CookieUtil.addCookie(response,"token",token,RedisKeys.USER_LOGIN_TOKEN.getTime().intValue());
        return AjaxResult.SUCCESS;
    }
}
